<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        setCookie('ww', '123')

        console.log(getCookies());


        console.log(getCookie('yy'));


        // setCookie('ww', '999')

        removeCookie('yy')

        // 创建cookie   /  修改有可以使用
        function setCookie(name, content, days = 7) {
            const date = new Date();
            // 解决8小时的时差
            date.setHours(date.getHours() - 8);
            // 把到期时间设置到days天以后
            date.setDate(date.getDate() + days);
            document.cookie = `${name}=${content};path=/;expires=${date}`
        }

        // 获取所有的cookie
        function getCookies() {
            const cookies = document.cookie;
            let arr = cookies.split('; ');
            arr = arr.map(item => {
                const arr2 = item.split('=');
                return {
                    name: arr2[0],
                    content: arr2[1]
                }
            })
            return arr
        }


        // 获取指定的cookie
        function getCookie(name) {
            const arr = getCookies();
            // 找到数据
            const res = arr.find(item => item.name === name);
            if (res) {
                return res.content
            }
            else {
                return ''
            }
        }


        // 删除cookie
        function removeCookie(name) {
            setCookie(name, '', -1);
        }


    </script>

</body>

</html>